/*******************************************************************************
 * Copyright (c) 2010 Red Hat, Inc.
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/
package ids.dev.managedbeans;

import ids.dev.business.InvoiceBO;
import ids.dev.business.SysUserBO;
import ids.dev.model.Group;
import ids.dev.model.Invoice;
import ids.dev.model.SysUser;
import ids.dev.util.IDSPagesUtil;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by JBoss Tools
 */
@Named
@ViewScoped
public class InvoiceMB implements Serializable{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Invoice entity;
	
	@Inject
	private InvoiceBO invoiceBO;
	
	@Inject
	private SysUserBO sysUserBO;
	
	private List<Invoice> selectedInvoices;
	private List<SysUser> listSysUser;
	private SysUser selectedUser;
	private Group selectedGroup;
	
	private Float totalSelectedInvoices;
	
	private boolean displayNewInvoiceForm;
	
	@PostConstruct
	public void init(){
		System.out.println("[IDS - LOG] Init GroupMB");
		if(this.entity == null){
			this.entity = new Invoice();
		}
		this.listSysUser = sysUserBO.findAll();
		this.displayNewInvoiceForm = Boolean.TRUE;
	}
	
	public List<Invoice> findAll(){
		return invoiceBO.findAll();
	}
	
	public String save(){
		this.entity.setPayer(this.selectedUser);
		invoiceBO.save(entity);
		return IDSPagesUtil.URL_INVOICE_VIEW;
	}
	
	public String  delete(){
		invoiceBO.delete(this.selectedInvoices);
		entity = new Invoice();
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Mazza!", "Nota Fiscal Removida Com Sucesso"));
		return IDSPagesUtil.URL_INVOICE_VIEW;
	}
	
	public void selectInvoice(Invoice entity){
		this.entity = entity;
	}
	
	
	public void calculateSelectedInvoices(){
		this.totalSelectedInvoices = new Float(0);
		for (Invoice invoice : this.selectedInvoices) {
			
			if(invoice.getTotal() != null){
				this.totalSelectedInvoices += invoice.getTotal();
			}
			
		}
		if(this.displayNewInvoiceForm){
			this.displayNewInvoiceForm = Boolean.FALSE;
		}else{
			this.displayNewInvoiceForm = Boolean.TRUE;
		}
	}
	
	// ################ GETTERS AND SETTERS ########################

	
	public Invoice getEntity() {
		return entity;
	}

	public void setEntity(Invoice entity) {
		this.entity = entity;
	}

	public List<Invoice> getSelectedInvoices() {
		return selectedInvoices;
	}

	public void setSelectedInvoices(List<Invoice> selectedInvoices) {
		this.selectedInvoices = selectedInvoices;
	}

	public List<SysUser> getListSysUser() {
		return listSysUser;
	}

	public void setListSysUser(List<SysUser> listSysUser) {
		this.listSysUser = listSysUser;
	}

	public SysUser getSelectedUser() {
		return selectedUser;
	}

	public void setSelectedUser(SysUser selectedUser) {
		this.selectedUser = selectedUser;
	}

	public Group getSelectedGroup() {
		return selectedGroup;
	}

	public void setSelectedGroup(Group selectedGroup) {
		this.selectedGroup = selectedGroup;
	}

	public Float getTotalSelectedInvoices() {
		return totalSelectedInvoices;
	}

	public void setTotalSelectedInvoices(Float totalSelectedInvoices) {
		this.totalSelectedInvoices = totalSelectedInvoices;
	}

	public boolean isDisplayNewInvoiceForm() {
		return displayNewInvoiceForm;
	}

	public void setDisplayNewInvoiceForm(boolean displayNewInvoiceForm) {
		this.displayNewInvoiceForm = displayNewInvoiceForm;
	}
	
	
	
	
}